package com.shxc.smarttrade.old.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.TradeCategory;
import com.shxc.smarttrade.old.myinterface.TradeCategoryItemClicklistener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/10.
 */

public class IndexFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    LayoutInflater mInflater;
    Context mContext;
    List<TradeCategory.DataBean.SymbolsBean> index;
    Map<Integer, Integer> map = new HashMap<>();
    private TradeCategoryItemClicklistener listener;


    public IndexFragmentAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setDatas(List<TradeCategory.DataBean.SymbolsBean> index) {
        this.index = index;
        for (int i = 0; i < index.size(); i++) {
            if (index.get(i).isFocus()) {
                map.put(i, 1);
            } else {
                map.put(i, 0);
            }

        }

        notifyDataSetChanged();
    }

    public void setMyItemClickListener(TradeCategoryItemClicklistener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new IndexViewholder(mInflater.inflate(R.layout.adapter_trade_category, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof IndexViewholder) {
            final TradeCategory.DataBean.SymbolsBean sb = index.get(position);
            if (map.get(position) == 0) {
                ((IndexViewholder) holder).ivStatus.setImageResource(R.mipmap.plus_gray);
            } else {
                ((IndexViewholder) holder).ivStatus.setImageResource(R.mipmap.right_mark);
            }

            ((IndexViewholder) holder).tvName.setText(sb.getSymbolCn());
            ((IndexViewholder) holder).tvEn.setText(sb.getSymbolEn());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (map.get(position) == 0) {
                        listener.CLickItem(sb, position);
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return index == null ? 0 : index.size();
    }

    public class IndexViewholder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_en)
        TextView tvEn;
        @BindView(R.id.iv_status)
        ImageView ivStatus;

        IndexViewholder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }
}
